/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.groupb.circus.Databases;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/**
 *
 * @author a
 */
public class PhotoHandler extends org.xml.sax.helpers.DefaultHandler {
    
    private Images image;
    private StringBuffer accumulator;
    String id="";
    String name ="";
    String lang="";
    String x="";
    String y="";
    String unit="";
    String unit_dos="";
    String unit_x="";
    String unit_y="";
    
    
    public PhotoHandler(){
        image = new Images();
    }
    
    public PhotoHandler (Images Ima){
        image = Ima;
    }
    
    // Called at the beginning
    @Override
    public void startDocument() {
	accumulator = new StringBuffer();
    }
    /*
     * When the parser encounters plain text (not XML elements), it calls this method, which accumulates 
     * the text in a string buffer. Note that this method may be called in multiple times.
     * 
     */
    
    @Override
    public void characters(char[] buffer, int start, int length) {
	accumulator.setLength(0);
        accumulator.append(buffer, start, length);
    }
    
    /*
     *
     * At the beginning of each new element, erase any accumulated text
     * 
     */
    @Override
    public void startElement(String namespaceURL, String localName, String qname, Attributes attributes) {
	
      accumulator.setLength(0);
        
        if(localName.equals("image")){
            id = attributes.getValue("id");
           // image.setId(this.id);
        }
        
        if(localName.equals("author")){
            name=attributes.getValue("name");
        }
        
        if(localName.equals("description")){
            lang=attributes.getValue("lang");
        }
        
        if(localName.equals("geotag")){
            x=attributes.getValue("x");
            y=attributes.getValue("y");
        }
        
        if(localName.equals("size")){
            unit=attributes.getValue("unit");
        }
        
       if(localName.equals("resolution")){
            unit_dos=attributes.getValue("unit");
        }
        
         if(localName.equals("x")){
            unit_x=attributes.getValue("unit");
        }
         
         if(localName.equals("y")){
            unit_y=attributes.getValue("unit");
        }
        
        
    }
    
    // Take special action when reach the end of selected elements. 
    // In this case the accumulated text is stored in the photo object
    @Override
    public void endElement(String namespaceURL, String localName, String qname){
	
        if (localName.equals("image")) {        
		image.setId(id);
                
        }
        
        if (localName.equals("title")){
            image.setTitle(accumulator.toString().trim());
        }
        
        
        if(localName.equals("day")){
            image.setDay(accumulator.toString().trim());
            
        }
        
        if(localName.equals("month")){
            image.setMonth(accumulator.toString().trim());
        }
        
        if(localName.equals("year")){
            image.setYear(accumulator.toString().trim());
        }
        
        if (localName.equals("path")){
            image.setPath(accumulator.toString().trim());
            
          }
        
        if (localName.equals("author")){
            image.setAuthor(name);
            
        }
        
        if (localName.equals("description")){
            image.setDescription(accumulator.toString().trim());
            
        }
        
        if (localName.equals("keyword")) {        
		String keyword = accumulator.toString().trim();
		image.addKeyword(keyword);
                      
	}
        
        if(localName.equals("continent")){
            image.setContinent(accumulator.toString().trim());
            
        }
        if(localName.equals("country")){
            image.setCountry(accumulator.toString().trim());
            
        }
        if(localName.equals("place")){
            image.setPlace(accumulator.toString().trim());
        }
       
        if(localName.equals("geotag")){
            image.setGeotagX(x);
            image.setGeotagY(y);
        }
        
        if(localName.equals("size")){
            image.setSize(accumulator.toString().trim()+" "+unit);
        }
        
        if(localName.equals("resolution")){
            image.setResolution(unit_dos +" "+accumulator.toString().trim());
        }
        
       if(localName.equals("x")){
            image.setDimensionsX(unit_x + accumulator.toString().trim());
        }
        
        if(localName.equals("y")){
            image.setDimensionsY(unit_y + accumulator.toString().trim());
        }
      
        if(localName.equals("format")){
            image.setFormat(accumulator.toString().trim());
        }
              
        
    }

    // Called at the end
    @Override
    public void endDocument() {
    }

    @Override
    public void warning(SAXParseException exception) {
	System.err.println("WARNING: line " + exception.getLineNumber() + ": "+
			   exception.getMessage());
    }

    @Override
    public void error(SAXParseException exception) {
	System.err.println("ERROR: line " + exception.getLineNumber() + ": " +
			   exception.getMessage());
    }

    @Override
    public void fatalError(SAXParseException exception) throws SAXException {
	System.err.println("FATAL: line " + exception.getLineNumber() + ": " +
			   exception.getMessage());
	throw(exception);
    }
    
}
